﻿CREATE PROCEDURE [acms].[UserPostUploadVote_Put]
	@Id int, 
	@UserId int,
	@UserPostUploadId int,
	@Score decimal(5, 4),
	@Created datetime,
	@ConcurencyId rowversion,
	@IdOut int output,
	@TitleOut varchar(500) output,
	@PostIdOut int output,
	@ConcurencyIdOut rowversion output
	
AS
	if exists(select null from UserPostUploadVote_View with (nolock) where UserId = @UserId and UserPostUploadId = @UserPostUploadId)
	begin
		update UserPostUploadVote
		set Score = @Score
		where UserId = @UserId and UserPostUploadId = @UserPostUploadId and ConcurencyId = @ConcurencyId
		
		if(@@rowcount = 0)
			return -100
	end
	else begin
		insert into UserPostUploadVote
			(UserId, UserPostUploadId, Score, Created)
		values
			(@UserId, @UserPostUploadId, @Score, @Created)
	end
	
	select @IdOut = Id, @TitleOut = Title, @PostIdOut = PostId, @ConcurencyIdOut = ConcurencyId From UserPostUploadVote_View with (nolock) where UserId = @Id and UserPostUploadId = @UserPostUploadId
RETURN 0;